﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTOFrameworks;
using System.Data.Common;
using FooFun.DataElements.Dtos;
using FooFun.DataElements;

namespace PhysicalDataProvider
{
    public class DataProviderUtil
    {
        public static string GetGuidBase64(object obj)
        {
            if (obj != null)
            {
                if (obj is Guid)
                {
                    Guid guid = (Guid)obj;
                    string guidStr = Convert.ToBase64String(
                        guid.ToByteArray());

                    return guidStr;
                }

                string str = obj.ToString();

                if (str.Length == 24)
                {
                    return str;
                }
                if (str.Length == 32)
                {
                    Guid guid = new Guid(str);

                    str = Convert.ToBase64String(
                        guid.ToByteArray());

                    return str;
                }
                if (str.Length == 36)
                {
                    Guid guid = new Guid(str);

                    str = Convert.ToBase64String(
                        guid.ToByteArray());

                    return str;
                }
            }

            throw new DataElementException("无法取出GUID数据，原始数据为" + obj);
        }

        public static Guid GetGuid(object obj)
        {
            if (obj != null)
            {
                if (obj is Guid)
                {
                    return (Guid)obj;
                }
                string str = obj.ToString();

                if (str.Length == 24)
                {
                    byte[] bytes = Convert.FromBase64String(str);

                    Guid guid = new Guid(bytes);

                    return guid;
                }
                else if (str.Length == 32)
                {
                    Guid guid = new Guid(str);

                    return guid;
                }
                else if (str.Length == 36)
                {
                    Guid guid = new Guid(str);

                    return guid;
                }
            }

            throw new DataElementException("无法取出GUID数据，原始数据为" + obj);
        }        

        public static IDataElement GetGuid(IDataElement de)
        {
            Guid guid = GetGuid(de.Value);

            return new ObjectDataElement(guid);
        }

        public static IDataElement GetGuidBase64(IDataElement de)
        {
            string str = GetGuidBase64(de.Value);

            return new StringDataElement(str);
        }
        public static string GetGuid32(object obj)
        {
            if (obj != null)
            {
                if (obj is Guid)
                {
                    Guid guid = (Guid)obj;

                    return guid.ToString("N");
                }

                string str = obj.ToString();

                if (str.Length == 24)
                {
                    Guid guid = GetGuid(str);

                    return guid.ToString("N");
                }
                if (str.Length == 32)
                {
                    return str;
                }
                if (str.Length == 36)
                {
                    Guid guid = new Guid(str);

                    return guid.ToString("N");
                }
            }

            throw new DataElementException("无法取出GUID数据，原始数据为" + obj);
        }

        public static IDataElement GetGuid32(IDataElement de)
        {
            string str = GetGuidBase64(de.Value);

            return new StringDataElement(str);
        }
        public static IDataElement GetInt(IDataElement de)
        {
            if (de.Value != null)
            {
                if (de.Value is int)
                {
                    return de;
                }
                try
                {
                    int i = Convert.ToInt32(de.Value);

                    return new IntDataElement(i);
                }
                catch { }
            }
            throw new DataElementException("无法取出整数类型数据，原始数据为" + de.Value);
        }

        public static IDataElement GetString(IDataElement de)
        {
            if (de.Value != null)
            {
                if (de.Value is int)
                {
                    return de;
                }

                string str = de.Value.ToString();
                return new StringDataElement(str);

            }
            throw new DataElementException("无法取出字符串类型数据，原始数据为" + de.Value);
        }
    }
}
